import localStorage from '@/common/js/store.js'

const modulesFiles = require.context('./navi', true, /\.js$/)
var data = [];
modulesFiles.keys().forEach(modulePath => {
  const navi = modulesFiles(modulePath)
  data = [...data, ...navi.default];
})

// 按module名称排序(注意不是文件名)
const sortKey = ['siteConfig', 'material', 'course', 'user', 'school', 'exam', 'mall', 'sns', 'content', 'market', 'finance', 'data', 'system'
];

sortKey.forEach((module, index) => {
  const moduleIndex = data.findIndex(item => {
    return item.module == module
  });
  if (moduleIndex != -1) {
    const current = data[moduleIndex]
    data.splice(moduleIndex, 1);
    data.splice(index, 0, current);
  }
});

export default {
  namespaced: true,
  state: {
    currentTopModule: 'home',
    datas: data,
    permissionNodes: localStorage.get('user.permissionNodes', [])
  },
  mutations: {
    SET_TOP_MODULE: (state, module) => {
      state.currentTopModule = module
    },
    SET_PERMISSION_NODES: (state, list) => {
      localStorage.set('user.permissionNodes', list)
      state.permissionNodes = list
    }
  },
  getters: {
    datas(state) {
      return state.datas;
    },
    nodes(state) {
      return state.permissionNodes;
    }
  },
  actions: {
    setPermissionNodes({
      commit
    }, {
      list
    }) {
      commit('SET_PERMISSION_NODES', list);
    }
  }
}
